package com.freelancer.musicinstruction.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.freelancer.musicinstruction.R;
import com.freelancer.musicinstruction.activity.delegates.DialogManager;
import com.freelancer.musicinstruction.activity.delegates.ThreadManager;
import com.freelancer.musicinstruction.activity.interfaces.ServiceInvokable;
import com.freelancer.musicinstruction.services.impl.youtube.YoutubeSearchService;
import com.freelancer.musicinstruction.services.interfaces.ISearchServices;
import com.freelancer.musicinstruction.tasks.YoutubeSearchTask;


public class SearchPage extends Activity implements ServiceInvokable {
	
	private static final String TAG = SearchPage.class.getSimpleName();

	private Button	searchButton;
	private EditText songTitle;
	private EditText bandName;
	private Spinner instrument;
	
	private DialogManager dm;

	protected int menuId = R.menu.menu;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchpage);
        
        dm = new DialogManager( this );
        
        findViews();
		setListeners();

    }

    
    private void findViews() {
		
		searchButton = (Button) findViewById( R.id.Button01 );
		songTitle = (EditText) findViewById( R.id.EditText01 );
		bandName = (EditText) findViewById( R.id.EditText02);
		instrument = (Spinner) findViewById( R.id.selected_instrument );
	}

    
	private void setListeners() {
		
		searchButton.setOnClickListener( new OnClickListener() {

			public void onClick(View v) {
				Toast t;
				
				if( (songTitle.getText()).toString().equalsIgnoreCase( "" )|| (songTitle.getText()).toString() == null ){
					t = Toast.makeText( SearchPage.this , "please specify a song title", Toast.LENGTH_SHORT);
					t.setGravity( android.view.Gravity.BOTTOM, 0, 0 );
					t.show();
				}
				else if ( ( bandName.getText()).toString().equalsIgnoreCase( "" ) || (bandName.getText()).toString() == null ){
					t = Toast.makeText( SearchPage.this , "please specify a band name", Toast.LENGTH_SHORT);
					t.setGravity( android.view.Gravity.BOTTOM, 0, 0);
					t.show();
				}
				else{
					
					Intent intent = new Intent( SearchPage.this, SearchResults.class);
					intent.putExtra( "song_name", songTitle.getText().toString());
					intent.putExtra( "band_name", bandName.getText().toString());
					intent.putExtra( "instrument", instrument.getSelectedItem().toString());
					startActivity( intent );
				}
			}
		});
	}

	
	@Override
	public void onServiceCallFailed() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onServiceCallFailedAborted() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onServiceCallFailedNoNetwork() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadingComplete() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadingStarting() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(menuId, menu);
		return true;
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.settings:
			startActivity(new Intent(this, Settings.class));
			break;
		case R.id.about:
			String title = "About Music Instruction";
			String message = "Created by Charles Akalugwu\n (M-Pire Technologies GmbH)\n\n" +
					"This application searches YouTube and 911Tabs for music instruction" +
					" material \nand consolidates the data" +
					"\nin an easy-to-use interface";
			showCustomDialog( title, message );
			break;
		}
		return false;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		return dm.onCreateDialog(id);
	}
	
	public void showCustomDialog(String title, String message) {
		dm.showCustomDialog(title, message);
	}

	@Override
	public void onLoadingInterrupted() {
		// TODO Auto-generated method stub
		
	}
	
	
}